package com.example.wechat_b.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wechat_b.R;
import com.example.wechat_b.activity.FriendActivity;
import com.example.wechat_b.bean.FriendListBean;

import java.util.List;

/**
 * @author liaoqg
 * @brief description
 * @date 2021-11-03
 */
public class FriendListViewAdapter extends BaseAdapter {

    private Context context;
    private List<FriendListBean> beanList;

    public FriendListViewAdapter(){}

    public FriendListViewAdapter(Context context, List<FriendListBean> beanList){
        this.context = context;
        this.beanList = beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.friend_item,parent,false);
            viewHolder.img = convertView.findViewById(R.id.friend_list_img);
            viewHolder.tv = convertView.findViewById(R.id.friend_list_item);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        FriendListBean bean = beanList.get(position);
        if (bean!=null){
            viewHolder.tv.setText(bean.txt);
            viewHolder.img.setImageResource(bean.img);
        }
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, FriendActivity.class);
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    class ViewHolder{
        public ImageView img;
        public TextView tv;
    }
}
